<?php
/**
 * 商品详情页控制器
 * @author e5
 *
 */
class ContentController extends CommonController{
	public function index() {
		$gid = Q("get.gid",0,"intval");
		if ($gid == 0) $this->error("商品不存在");
		//获得全部分类
		$cateData = K("Category")->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		//获得商品信息
		$goodsData = M()->join("__goods__ g JOIN __detail__ d ON g.gid=d.vshop_goods_gid")->where("g.gid={$gid}")->find();
		$goodsData['bigimg'] = explode(",", $goodsData['big']);
		$goodsData['medimg'] = explode(",", $goodsData['medium']);
		$goodsData['smallimg'] = explode(",", $goodsData['small']);
		//获得商品属性
		$attr = M()->join("__goodsattr__ ga JOIN __typeattr__ ta ON ga.vshop_typeattr_taid=ta.taid")->where("ga.vshop_goods_gid={$gid}")->all();
		$this->assign("attr",$attr);
		//组合可选商品属性数组
		$attrSel = array();
		foreach ($attr as $k=>$atv){
			if ($atv['tatype'] == 1){
				$attrSel[$atv['taid']]['name'] = $atv['taname'];
				$attrSel[$atv['taid']]['value'][] = $atv;
			}
		}
		//为页面设置默认商品属性参数***
		$atNum = 0;
		//参数默认数组
		$paramTemp = array();
		foreach ($attrSel as $atsk=>$atsv){
			$attrSel[$atsk]['atnum'] = $atNum;
			$atNum++;
			$paramTemp[] = $atsv['value'][0]['gaid'];
		}
		$this->assign("attrSel",$attrSel);
		//筛选参数***
		//统计参数个数(参数个数与$attr元素个数相等)
		$pTotal = count($attrSel);
		//参数存在就通过"_"拆分成数组(数组键值为商品属性id)，不存在就设置为默认
		$param = isset($_GET['param'])?explode("_", $_GET['param']):$paramTemp;
		//如果用户手动删除或增加参数，将参数设置为默认
		if (count($param) != $pTotal) $param = $paramTemp;
		$this->assign("param",$param);
		//处理已选属性，用于页面显示
		$selAttr = array();
		foreach ($attr as $gaid){
			if (in_array($gaid['gaid'], $param)){
				$selAttr[] = $gaid['gavalue'];
			}
		}
		$this->assign("selAttr",$selAttr);
		//获得货品数据
		$combine = "";
		foreach ($param as $v){
			$combine .= "combine LIKE '%{$v}%' AND ";
		}
		$combine = rtrim($combine, "AND ");
		$attrGoods = K("GoodsList")->where($combine)->find();
		$this->assign("attrGoods",$attrGoods);
		//处理附加价格
		$adwhere = implode(",", $param);
		$addPrice = K("GoodsAttr")->where("gaid IN ({$adwhere})")->sum('addprice');
		$goodsData['gshopprice'] = number_format($goodsData['gshopprice']+intval($addPrice),2);
		$this->assign("goodsData",$goodsData);
		//面包屑导航
		$fatherCateArr = $this->getFatherCate($cateData, $goodsData['vshop_category_cid']);
		//返转数组
		$fatherCateArr = array_reverse($fatherCateArr);
		$this->assign("fatherCateArr", $fatherCateArr);
		$getParam = implode("_", $param);
		$this->assign('getParam',$getParam);
		$this->dis("content");
	}
}